package com.itheima.app.exception;

import com.itheima.vo.ErrorResult;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice  // 全局异常处理器
public class GlobalExceptionAdvice {

    //处理自定义的异常
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity handlerBusinessException(BusinessException businessException) {
        //打印异常
        businessException.printStackTrace();

       //返回错误获取自定义异常信
        String message = businessException.getMessage();
        switch (message) {
            case "1":
                return ResponseEntity.status(500).body(ErrorResult.unLoginError());
            case "2":
                return ResponseEntity.status(500).body(ErrorResult.commentError());
            case "3":
                return ResponseEntity.status(500).body(ErrorResult.movementError());
            default:
                return ResponseEntity.status(500).body(ErrorResult.error());
        }

    }

    // 捕获exception异常
    @ExceptionHandler(Exception.class)
    public ResponseEntity exceptionHandlerMethod(Exception ex) {
        // 将异常信息输出到控制台
        ex.printStackTrace();
        // 友情提示
        return ResponseEntity.status(500).body(ErrorResult.error());
    }

}